package com.woyou.ui.activity;

import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import com.citaq.ideliver.R;
import com.woyou.bean.Info;
import com.woyou.bean.MyAddress;
import com.woyou.ui.adapter.AddrListAdapter;
import com.woyou.ui.adapter.AddrListAdapter.AddrSelectListener;

import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 我的地址界面MyAddrFragment
 * 
 * @author tanglc
 */
@EActivity(R.layout.fm_addrs)
public class SelectAddrActivity extends BaseActivity {

	List<MyAddress> list;
	@ViewById
	RelativeLayout myback;
	@ViewById
	TextView head_title;
	@ViewById
	ListView addr_list;
	@ViewById
	Button addbtn;

	AddrListAdapter addrListAdapter;

	@AfterViews
	public void init() {
		// 初始化数据
		initView();
	}

	@Override
	public void onResume() {
		super.onResume();
		RefreshListView();
		addrListAdapter.notifyDataSetChanged();
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	@Click({ R.id.myback, R.id.addbtn })
	void loginOnClick(View view) {
		switch (view.getId()) {
		case R.id.myback:
			// 退出地址管理界面
			finish();
			break;
			
		case R.id.addbtn:
			// 进入添加地址界面
			openActivity(PutAddrActivity_.class, null);
			break;
		default:
			break;
		}
	}

	public void initView() {
		head_title.setText("选择配送地址");
	}

	public void RefreshListView() {
		list = mUserModel.queryAddrList();
		addrListAdapter = new AddrListAdapter(list, mContext);
		addrListAdapter.setAddrSelectListener(new AddrSelectListener() {

			@Override
			public void addrSelect(MyAddress addr) {
				Info<MyAddress> info = new Info<MyAddress>(addr);
				info.setOrigin(MyAddrActivity_.class);
				closeActivity4Result(1, info);
				finish();
			}
		});
		addr_list.setAdapter(addrListAdapter);
	}

	@Override
	public boolean backOperate() {
		// TODO Auto-generated method stub
		return false;
	}

}
